package com.maibow.controller.admin;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.maibow.controller.base.BaseController;
import com.maibow.entity.Page;
import com.maibow.entity.admin.Dictionary;
import com.maibow.entity.admin.DictionaryCatalog;
import com.maibow.service.admin.DictionaryService;
import com.maibow.util.PageData;

/**
 * 
 * 脉博网络后台管理系统
 * 包名：com.maibow.controller.system
 * 文件名：DictionariesController.java
 * 作者: 吴比(Wubi)
 * 时间：2015年4月2日-上午9:07:54
 * Copyright@ 2015湖南脉博网络科技有限公司-版权所有
 *
 */
@Controller
@RequestMapping(value = "/dictionary")
public class DictionaryController extends BaseController {

    @Resource(name = "dictionaryService")
    private DictionaryService dictionaryService;

    /**
     * 
     * list(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param page
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView list(Page page) throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = new PageData();
        pageData = this.getPageData();
        page.setPageData(pageData);
        List<DictionaryCatalog> dictionaryCatalogList = dictionaryService.queryAllCataloglistPage(page);
        modelView.setViewName("system/dictionary/dictionary_catalog_list");
        modelView.addObject("pageData", pageData);
        modelView.addObject("dictionaryCatalogList", dictionaryCatalogList);
        return modelView;
    }

    /**
     * 
     * listDictionary(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param catacode
     * 标记：@param page
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/listDictionary/{catacode}", method = RequestMethod.GET)
    public ModelAndView listDictionary(@PathVariable String catacode, Page page) throws Exception {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = new PageData();
        pageData = this.getPageData();
        pageData.put("catacode", catacode.replace("'", ""));
        page.setPageData(pageData);
        List<Dictionary> dictionaryList = dictionaryService.queryAllDictionarylistPage(page);
        modelView.setViewName("system/dictionary/dictionary_list");
        modelView.addObject("pageData", pageData);
        modelView.addObject("dictionaryList", dictionaryList);
        return modelView;
    }

    /**
     * 
     * toAddDictionaryCatalog(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param page
     * 标记：@return
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/toAddDictionaryCatalog")
    public ModelAndView toAddDictionaryCatalog(Page page) {
        ModelAndView modelView = this.getModelAndView();
        try {
            modelView.setViewName("system/dictionary/dictionary_catalog_add");
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }

        return modelView;
    }

    /**
     * 
     * toAddDictionary(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param page
     * 标记：@return
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/toAddDictionary/{catacode}")
    public ModelAndView toAddDictionary(@PathVariable String catacode, Page page) {
        ModelAndView modelView = this.getModelAndView();
        PageData pageData = this.getPageData();
        pageData.put("catacode", catacode);
        try {
            modelView.setViewName("system/dictionary/dictionary_add");
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }

        modelView.addObject("pageData", pageData);
        return modelView;
    }

    /**
     * 
     * saveDictionaryCatalog(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param out
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/saveDictionaryCatalog", method = RequestMethod.POST)
    public ModelAndView saveDictionaryCatalog(DictionaryCatalog dictionaryCatalog, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView modelView = this.getModelAndView();
        dictionaryService.saveDictionaryCatalog(dictionaryCatalog);
        modelView.addObject("msg", "success");
        modelView.setViewName("save_result");
        return modelView;
    }

    /**
     * 
     * saveDictionary(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param dictionary
     * 标记：@param request
     * 标记：@param response
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/saveDictionary", method = RequestMethod.POST)
    public ModelAndView saveDictionary(Dictionary dictionary, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView modelView = this.getModelAndView();
        dictionaryService.saveDictionary(dictionary);
        modelView.addObject("msg", "success");
        modelView.setViewName("save_result");
        return modelView;
    }

    /**
     * 
     * toEditDictionaryCatalog(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param cataid
     * 标记：@param request
     * 标记：@param response
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/toEditDictionaryCatalog/{cataid}")
    public ModelAndView toEditDictionaryCatalog(@PathVariable String cataid, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView modelView = this.getModelAndView();
        DictionaryCatalog dictionaryCatalog = dictionaryService.findDictionaryCatalogById(Integer.valueOf(cataid));
        modelView.setViewName("system/dictionary/dictionary_catalog_edit");
        modelView.addObject("dictionaryCatalog", dictionaryCatalog);
        return modelView;
    }

    /**
     * 
     * toEditDictionary(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param dicid
     * 标记：@param request
     * 标记：@param response
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/toEditDictionary/{dicid}")
    public ModelAndView toEditDictionary(@PathVariable String dicid, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView modelView = this.getModelAndView();
        Dictionary dictionary = dictionaryService.findDictionaryById(Integer.valueOf(dicid));
        modelView.setViewName("system/dictionary/dictionary_edit");
        modelView.addObject("dictionary", dictionary);
        return modelView;
    }

    /**
     * 
     * editDictionaryCatalog(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param dictionaryCatalog
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/editDictionaryCatalog", method = RequestMethod.POST)
    public ModelAndView editDictionaryCatalog(DictionaryCatalog dictionaryCatalog) throws Exception {
        ModelAndView modelView = this.getModelAndView();
        dictionaryService.editDictionaryCatalog(dictionaryCatalog);
        modelView.addObject("msg", "success");
        modelView.setViewName("save_result");
        return modelView;
    }

    /**
     * 
     * editDictionary(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param dictionary
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @RequestMapping(value = "/editDictionary", method = RequestMethod.POST)
    public ModelAndView editDictionary(Dictionary dictionary) throws Exception {
        ModelAndView modelView = this.getModelAndView();
        dictionaryService.editDictionary(dictionary);
        modelView.addObject("msg", "success");
        modelView.setViewName("save_result");
        return modelView;
    }

    /**
     * 
     * deleteDictionaryCatalog(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param out
     * 标记：@param cataid
     * 标记：@param request
     * 标记：@param response
     * 返回值：void
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @ResponseBody
    @RequestMapping(value = "/deleteDictionaryCatalog/{cataid}/{catacode}", method = RequestMethod.DELETE)
    public ModelAndView deleteDictionaryCatalog(@PathVariable String cataid, @PathVariable String catacode, HttpServletRequest request, HttpServletResponse response) {
        PageData pageData = new PageData();
        ModelAndView modelView = new ModelAndView();
        String msg = null;
        try {
            pageData.put("catacode", catacode);
            if (dictionaryService.findDictionaryByCataCodeCount(pageData) != null && dictionaryService.findDictionaryByCataCodeCount(pageData).getInteger("dictionarycount") > 0) {
                msg = "error";
            } else {
                dictionaryService.deleteDictionaryCatalog(Integer.valueOf(cataid));
                msg = "success";
            }

        } catch (Exception e) {
            logger.error(e.toString(), e);
        }

        modelView.addObject(msg);
        return modelView;
    }

    /**
     * 
     * deleteDictionaryCatalog(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param dicid
     * 标记：@param request
     * 标记：@param response
     * 标记：@return
     * 返回值：Object
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @ResponseBody
    @RequestMapping(value = "/deleteDictionary/{dicid}", method = RequestMethod.DELETE)
    public ModelAndView deleteDictionary(@PathVariable String dicid, HttpServletRequest request, HttpServletResponse response) {
        String msg = null;
        ModelAndView modelView = new ModelAndView();
        try {
            dictionaryService.deleteDictionary(Integer.valueOf(dicid));
            msg = "success";
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }

        modelView.addObject(msg);
        return modelView;
    }

    /**
     * 
     * hasCataCode(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param catacode
     * 标记：@param cataid
     * 标记：@param request
     * 标记：@param response
     * 标记：@return
     * 返回值：ModelAndView
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @ResponseBody
    @RequestMapping(value = "/hasCataCode/{catacode}/{cataid}", method = RequestMethod.PUT)
    public ModelAndView hasCataCode(@PathVariable String catacode, @PathVariable String cataid, HttpServletRequest request, HttpServletResponse response) {
        PageData pageData = new PageData();
        ModelAndView modelView = new ModelAndView();
        String msg = null;
        try {
            pageData.put("catacode", catacode);
            pageData.put("cataid", Integer.valueOf(cataid));
            if (dictionaryService.findCataCodeCount(pageData) != null && dictionaryService.findCataCodeCount(pageData).getInteger("catacodecount") > 0) {
                msg = "error";
            } else {
                if (dictionaryService.findDictionaryByCataCodeCount(pageData) != null && dictionaryService.findDictionaryByCataCodeCount(pageData).getInteger("dictionarycount") > 0) {
                    msg = "hasDicionaryCount";
                } else {
                    msg = "success";
                }
            }
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }

        modelView.addObject(msg);
        return modelView;
    }

    @ResponseBody
    @RequestMapping(value = "/hasDicCode/{catacode}/{dicvalue}/{dicid}", method = RequestMethod.PUT)
    public ModelAndView hasDicCode(@PathVariable String catacode, @PathVariable String dicvalue, @PathVariable String dicid, HttpServletRequest request, HttpServletResponse response) {
        PageData pageData = new PageData();
        ModelAndView modelView = new ModelAndView();
        String msg = null;
        try {
            pageData.put("catacode", catacode);
            if (dicid != null && dicid.equals("")) {
                pageData.put("dicid", Integer.valueOf(dicid));
                pageData.put("dicvalue", dicvalue);
                if (dictionaryService.findDicCodeCount(pageData) != null && dictionaryService.findDicCodeCount(pageData).getInteger("diccodecount") > 0) {
                    msg = "error";
                } else {
                    msg = "success";
                }
            } else {
                msg = "success";
            }
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }

        modelView.addObject(msg);
        return modelView;
    }
}
